// 功能点： 
// 1. 音乐信息
// 2. 喜欢音乐
// 3. 音乐评论



// 1. 音乐信息



// 获取单曲详细信息的函数
function songDetail(id, userId) {
    ajaxRequest({
        url: `${defaultUrlHeader}/song/detail`,

        data: {
            ids: id
        },

        success: function (responseText) {
            console.log('获取单曲详细信息成功');
            console.log(responseText);

            // 渲染单曲信息
            songRender(responseText.songs[0], userId);
        },

        error: function (xhr) {
            console.log('获取单曲详细信息失败');
            console.log(xhr.readyState);
            console.log(xhr.status);
        }
    });
}



// 获取单曲评论
function songComments(id) {
    ajaxRequest({
        url: `${defaultUrlHeader}/comment/music`,

        data: {
            id: id,
            limit: 100
        },

        success: function (responseText) {
            console.log('获取单曲评论成功');
            console.log(responseText);

            // 渲染单曲评论
            songCommentsRender(responseText.hotComments, responseText.comments, responseText.userId);
        },

        error: function (xhr) {
            console.log('获取单曲评论失败');
            console.log(xhr.readyState);
            console.log(xhr.status);
        }
    })
}



// 单曲所在专辑图片
let songAlbumPicture = document.querySelector('.song-album-pic');
// 单曲名
let songName = document.querySelector('.song-name');
// 单曲歌手
let songPlayer = document.querySelector('.song-player');
// 单曲所在专辑名
let songAlbum = document.querySelector('.song-album');
// 单曲发行时间
let songPublishTime = document.querySelector('.song-publishtime');
// “播放”按钮
let displayMusic = document.querySelector('.display-music');
// “喜欢”按钮
let likeMusic = document.querySelector('.song-like');
// 评论框
let commentBox = document.querySelector('.song-display-area').querySelector('.comment-text');
// “发布评论”按钮
const publishCommentBtn = document.querySelector('.song-display-area').querySelector('.comment-button');
// 评论数
let commentNumber = document.querySelector('.song-display-area').querySelector('.comment-counts');
// 评论item
let commentsEach = document.querySelector('.song-display-area').querySelectorAll('.hot-comments');
// 用户头像
let commentHead = document.querySelector('.song-display-area').querySelectorAll('.comment-user-head');
// 用户昵称
let commentName = document.querySelector('.song-display-area').querySelectorAll('.comment-user-name');
// 评论时间
let commentsTime = document.querySelector('.song-display-area').querySelectorAll('.comment-date');
// 评论内容
let commentContents = document.querySelector('.song-display-area').querySelectorAll('.comment-content');
// 评论点赞
let commentLikes = document.querySelector('.song-display-area').querySelectorAll('.comment-like');
// "精彩评论"
let hotCommentsTitle = document.querySelector('.song-display-area').querySelector('.hot-comments-title');
// "我的评论"
let myCommentBox = document.querySelector('.song-display-area').querySelector('.my-comment');
// 我的头像
let myCommentHead = document.querySelector('.song-display-area').querySelector('.my-comment-head');
// 我的昵称
let myCommentName = document.querySelector('.song-display-area').querySelector('.my-comment-name');
// 评论时间
let myCommentTime = document.querySelector('.song-display-area').querySelector('.my-comment-date');
// 评论内容
let mycommentContent = document.querySelector('.song-display-area').querySelector('.my-comment-content');
// 评论点赞
let myCommentLike = document.querySelector('.song-display-area').querySelector('.my-comment-like');
// 无评论提示
let noSongCommentTip = document.querySelector('.song-display-area').querySelector('.no-comment-tip');
// 删除评论
let deleteSongCommentBtn = document.querySelector('.delete-song-comment-btn');
// 音乐播放页
const displayMusicPage = document.querySelector('.display-music-page');



// 渲染单曲信息
function songRender(song, userId) {

    songAlbumPicture.style.backgroundImage = `url(${song.al.picUrl})`;
    songName.innerHTML = song.name;
    songPlayer.innerHTML = `歌手：${song.ar[0].name}`;
    songAlbum.innerHTML = `专辑：${song.al.name}`;

    // "发布评论"点击事件
    publishCommentBtn.addEventListener('click', function () {

        let commentContent = commentBox.value;

        if (commentContent !== '') {
            // 发送评论请求
            ajaxRequest({
                url: `${defaultUrlHeader}/comment`,

                data: {
                    t: 1,
                    type: 0,
                    id: song.id,
                    content: commentContent
                },

                success: function (responseText) {
                    console.log('发送评论成功');
                    console.log(responseText);

                    // 显示用户评论
                    userSongCommentsRender(responseText.comment);
                },

                error: function (xhr) {
                    console.log('发送评论失败');
                    console.log(xhr.readyState);
                    console.log(xhr.status);
                }
            });
        } else {
            noSongCommentTip.style.display = 'block';

            commentBox.addEventListener('focus', () => {
                noSongCommentTip.style.display = 'none';
            })
        }
    })

    // "删除评论"点击事件
    deleteSongCommentBtn.addEventListener('click', () => {
        // 删除评论请求
        ajaxRequest({
            url: `${defaultUrlHeader}/comment`,

            data: {
                t: 0,
                type: 0,
                id: song.id,
                commentId: mycommentContent.getAttribute('data-songCommentId')
            },

            success: function (responseText) {
                console.log('删除歌曲评论成功');
                console.log(responseText);

                // 清空评论内容
                deleteSongComment();
            },

            error: function (xhr) {
                console.log('删除歌曲评论失败');
                console.log(xhr.readyState);
                console.log(xhr.status);
            }
        })
    })

    // 点击"播放"，跳至音乐播放页面
    displayMusic.addEventListener('click', function () {
        // 把单曲id和用户id传过去
        location.assign(`music.html?id=${song.id}`);
    })

    // 获取用户喜欢的音乐id列表
    likelist(song.id, userId);
}



// 2. 喜欢音乐



// 获取用户喜欢的音乐id列表
function likelist(songId, userId) {
    ajaxRequest({
        url: `${defaultUrlHeader}/likelist`,

        data: {
            uid: userId
        },

        success: function (responseText) {
            console.log('获取用户喜欢id列表成功');
            console.log(responseText);

            // 判断用户是否喜欢了这首歌
            isLiked(songId, responseText.ids);
        },

        error: function (xhr) {
            console.log('获取用户喜欢id列表失败');
            console.log(xhr.readyState);
            console.log(xhr.status);
        }
    })
}



// 判断用户是否喜欢了这首歌
function isLiked(songId, ids) {

    // 判断变量
    let flag = false,
        idsLength = ids.length;

    for (let i = 0; i < idsLength; i++) {
        if (songId == ids[i] && !flag) {

            flag = true;

            likeMusic.innerHTML = '已喜欢';
            likeMusic.style.backgroundColor = '#ff4572';
        }
    }

    likeMusic.addEventListener('click', () => {
        if (flag) {
            // 取消喜欢该单曲
            ajaxRequest({
                url: `${defaultUrlHeader}/like`,

                data: {
                    id: songId,
                    like: false
                },

                success: function (responseText) {
                    console.log('已成功取消喜欢该单曲');
                    console.log(responseText);

                    likeMusic.innerHTML = '喜欢';
                    likeMusic.style.backgroundColor = '#31C27C';
                },

                error: function (xhr) {
                    console.log('未成功取消喜欢该单曲');
                    console.log(xhr.readyState);
                    console.log(xhr.status);
                }
            })

            flag = false;

        } else {
            // 喜欢该单曲
            ajaxRequest({
                url: `${defaultUrlHeader}/like`,

                data: {
                    id: songId,
                    like: true
                },

                success: function (responseText) {
                    console.log('已成功喜欢该单曲');
                    console.log(responseText);

                    likeMusic.innerHTML = '已喜欢';
                    likeMusic.style.backgroundColor = '#ff4572';
                },

                error: function (xhr) {
                    console.log('未成功喜欢该单曲');
                    console.log(xhr.readyState);
                    console.log(xhr.status);
                }
            })

            flag = true;

        }
    })
}



// 3. 音乐评论



// 渲染单曲评论的函数
function songCommentsRender(hotComments, comments, userId) {

    let commentsLength = comments.length,
        hotCommentsLength = hotComments.length;

    // 判断评论数
    if (commentsLength) {
        commentNumber.innerHTML = `共${commentsLength}条评论`;
    } else {
        commentNumber.innerHTML = `共0条评论`;
    }

    // 先隐藏所有的评论item盒子
    for (let i = 0; i < 5; i++) {
        commentsEach[i].style.display = 'none';
    }

    // 优先显示热评，热评数不足5则用全部评论补全
    if (hotComments && hotCommentsLength >= 5) {
        // 渲染精彩评论
        songCommentDisplay(hotComments, 5);
    } else if (hotCommentsLength > 0 && hotCommentsLength < 5) {
        // 渲染精彩评论
        songCommentDisplay(hotComments, hotCommentsLength);
    } else if (comments && commentsLength > 0) {
        // 把"精彩评论"改为"全部评论"，但只显示五条
        hotCommentsTitle.innerHTML = '全部评论';
        for (let i = 0; i < commentsLength; i++) {
            // 无精彩评论
            commentsEach[i].style.display = 'block';
            commentHead[i].style.backgroundImage = `url(${comments[i].user.avatarUrl})`;
            commentName[i].innerHTML = comments[i].user.nickname;
            commentsTime[i].innerHTML = comments[i].timeStr;
            commentContents[i].innerHTML = comments[i].content;
            commentLikes[i].innerHTML = `${comments[i].likedCount}人觉得很赞`;
        }
    } else {
        // 无评论
        commentsEach[0].style.display = 'block';
        commentContents[0].innerHTML = '当前歌曲暂无评论';
    }

    // 判断用户是否有在此歌下面评论
    if (commentsLength) {
        for (let i = 0; i < commentsLength; i++) {
            if (comments[i].user.userId == userId) {
                // 显示"我的评论"盒子并进行渲染
                myCommentBox.style.display = 'block';
                myCommentHead.style.backgroundImage = `url(${comments[i].user.avatarUrl})`;
                myCommentName.innerHTML = comments[i].user.nickname;
                myCommentTime.innerHTML = comments[i].timeStr;
                mycommentContent.innerHTML = comments[i].content;
                myCommentLike.innerHTML = `${comments[i].likedCount}人觉得很赞`;
            }
        }
    }
}



// 渲染精彩评论
function songCommentDisplay(hotComments, length) {
    for (let i = 0; i < length; i++) {
        commentsEach[i].style.display = 'block';
        commentHead[i].style.backgroundImage = `url(${hotComments[i].user.avatarUrl})`;
        commentName[i].innerHTML = hotComments[i].user.nickname;
        commentsTime[i].innerHTML = hotComments[i].timeStr;
        commentContents[i].innerHTML = hotComments[i].content;
        commentLikes[i].innerHTML = `${hotComments[i].likedCount}人觉得很赞`;
    }
}



// 显示用户评论
function userSongCommentsRender(comment) {
    // 清空评论框内容
    commentBox.value = '';

    // 显示"我的评论"盒子并进行渲染
    myCommentBox.style.display = 'block';
    myCommentHead.style.backgroundImage = `url(${comment.user.avatarUrl})`;
    myCommentName.innerHTML = comment.user.nickname;
    myCommentTime.innerHTML = getTime(comment.time);
    mycommentContent.innerHTML = comment.content;
    myCommentLike.innerHTML = '0人觉得很赞';
    mycommentContent.setAttribute('data-songCommentId', comment.commentId);
}



// 清空歌曲评论
function deleteSongComment() {
    // 移除自定义属性评论id属性
    mycommentContent.removeAttribute('data-songCommentId');
    myCommentBox.style.display = 'none';
}